﻿using System.Web.UI.WebControls;
using System.Web;

namespace Sex
{
    public partial class Sex : System.Web.UI.Page
    {
        public static class Tree
        {
            public static string GetUrl(TreeNode tn, string[] RelativePathComponents, string ApplicationPath)
            {
                string PathReduced = GetPathReduced(tn, RelativePathComponents, ApplicationPath);
                HttpContext myHttpContext = HttpContext.Current;
                HttpRequest r = myHttpContext.Request;
                return r.Url.Scheme + "://" + r.Url.Authority + "/" + PathReduced;
            }
            public static string GetPathReduced(TreeNode tn, string[] RelativePathComponents, string ApplicationPath)
            {
                // correct Path
                string Path = Tree.Path(tn);
                string[] PathComponents = Path.Split(new char[] { '/' });
                int i = 0;
                string PathReduced = "";
                for (i = 0; i < RelativePathComponents.Length; i++)
                {
                    if (RelativePathComponents[i] != "..")
                    {
                        for (int j=i; j<RelativePathComponents.Length; j++)
                        PathReduced += RelativePathComponents[j] + "/";
                        break;
                    }
                }
                for (int j = i; j < PathComponents.Length; j++)
                {
                    PathReduced += PathComponents[j] + "/";
                }
                ItemInfo ii = new ItemInfo(tn.Value);
                if (ii.type == E_ObjectType.FILE) PathReduced = PathReduced.Substring(0, PathReduced.Length - 1);
                return ApplicationPath + PathReduced;
            }
            public static void UnselectNodes(TreeNode n)
            {
                n.Selected = false;
                for (int i = 0; i < n.ChildNodes.Count; i++)
                    UnselectNodes(n.ChildNodes[i]);
            }
            public static string Path(TreeNode tn)
            {
                string delimiter = "/";
                string path = "";
                do
                {
                    if (tn.Parent == null) // root, do not include in path
                    {
                        break;
                    }
                    path = tn.Text + (path == "" ? "" : delimiter) + path;
                    tn = tn.Parent;
                }
                while (true);
                return path;
            }
            public static string PathWR(TreeNode tn)
            {
                string delimiter = "/";
                string path = "";
                do
                {
                    path = tn.Text + (path == "" ? "" : delimiter) + path;
                    if (tn.Parent == null) 
                    {
                        break;
                    }
                    tn = tn.Parent;
                }
                while (true);
                return path;
            }
            public static void Expand(TreeNode tn, int Level)
            {
                if (Level >= tn.Depth) tn.Expand(); else tn.Collapse();
                for (int i = 0; i < tn.ChildNodes.Count; i++)
                    Expand(tn.ChildNodes[i], Level);
            }
            public static void ExpandPath(TreeNode tn)
            {
                if (tn == null) return;
                do
                {
                    tn.Expand();
                    if (tn.Parent == null) // root, do not include in path
                    {
                        break;
                    }
                    tn = tn.Parent;
                }
                while (true);
            }
            public static bool Search(TreeNode tn, string s)
            {
                bool Found = false;
                if (tn == null) return false;
                if (tn.Text.ToLower().Contains(s.ToLower()))
                {
                    Found = true;
                    tn.Checked = true;
                    ExpandPath(tn);
                }
                for (int i = 0; i < tn.ChildNodes.Count; i++)
                    Found = Found || Search(tn.ChildNodes[i], s);
                return Found;
            }
            public static long Size(TreeNode tn)
            {
                long value_currentdirectory = 0;
                for (int i = 0; i < tn.ChildNodes.Count; i++)
                {
                    if (tn.ChildNodes[i].Value.StartsWith("d"))
                    {
                        value_currentdirectory += Size(tn.ChildNodes[i]);
                    }
                    else
                    {
                        string v = tn.ChildNodes[i].Value;
                        string[] f = v.Split(new char[] { ValueDelimiter[0] });
                        value_currentdirectory += long.Parse(f[1]);
                    }
                }
                return value_currentdirectory;
            }
        }
    }
}